package util;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import com.alibaba.fastjson.JSONObject;

/**
 * <pre>
 *  File: HttpClientTest.java
 *  Xidian, Inc.
 *  Copyright (C): 2014
 *  Description:
 *  关于http请求工具类
 * 
 *  Notes:
 *  $Id: HttpClientTest.java 69862260-0 2016-06-09 13:59:59Z Xidian\lin.cy $ 
 * 
 * @Revision History:
 * <<2015年06月09日上午11:39:56>>, <<Who>>, <<what>>  
 *  2015年06月09日		lin.cy		Initial.
 * </pre>
 */
public class KKHttpClient
{
	public static KKResponse urlPostMethod4NoToken(String url, JSONObject jsonObject)
	{
		KKResponse response = new KKResponse();
		if(url == null || "".equals(url) && url.indexOf("kitkatback/project/id") == -1) {
			response.setStatusCode("500");
			response.setMessage("请输入正确的URL");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		String params = "";
		if (!"".equals(jsonObject) && null != jsonObject)
		{
			params = jsonObject.toJSONString();
		}
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		PostMethod method = new PostMethod(url);
		try
		{
			if (null != params && !params.trim().equals(""))
			{
				RequestEntity requestEntity = new StringRequestEntity(params, "application/json", "UTF-8");
				method.setRequestEntity(requestEntity);
			}
			httpClient.executeMethod(method);
			int statusCode = method.getStatusCode();
			String responses = method.getResponseBodyAsString();
			response.setStatusCode(statusCode+"");
			response.setResponseBody(responses);
			return response;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(e instanceof org.apache.commons.httpclient.ConnectTimeoutException){
				response.setStatusCode("408");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			} else{
				response.setStatusCode("500");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			}
			
		}
		finally
		{
			method.releaseConnection();
			
		}
		return response;
		
	}
	
	public static KKResponse urlPostMethod(String url, JSONObject jsonObject, String token)
	{
		KKResponse response = new KKResponse();
		if(token == null || "".equals(token) && url.indexOf("kitkatback/project/id") == -1){
			response.setStatusCode("500");
			response.setMessage("缺少Token认证信息");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		if(url == null || "".equals(url)) {
			response.setStatusCode("500");
			response.setMessage("请输入正确的URL");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		String params = "";
		if (!"".equals(jsonObject) && null != jsonObject)
		{
			params = jsonObject.toJSONString();
		}
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		PostMethod method = new PostMethod(url);
		try
		{
			if (params != null && !params.trim().equals(""))
			{
				RequestEntity requestEntity = new StringRequestEntity(params, "application/json", "UTF-8");
				method.setRequestEntity(requestEntity);
			}
			method.setRequestHeader("token", token);
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			int statusCode = method.getStatusCode();
			response.setStatusCode(statusCode+"");
			response.setResponseBody(responses);
			return response;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(e instanceof org.apache.commons.httpclient.ConnectTimeoutException){
				response.setStatusCode("408");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			} else{
				response.setStatusCode("500");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			}
		}
		finally
		{
			method.releaseConnection();
		}

		return response;
	}

	public static KKResponse urlGetMethod(String url, JSONObject params, String token)
	{
		KKResponse response = new KKResponse();
		if(ValidationUtil.isEmpty(token) && url.indexOf("kitkatback/project/id") == -1){
			response.setStatusCode("500");
			response.setMessage("缺少Token认证信息");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		if(ValidationUtil.isEmpty(url)) {
			response.setStatusCode("500");
			response.setMessage("请输入正确的URL");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		GetMethod method = new GetMethod(url);
		method.setRequestHeader("token", token);
		if(params != null && params.size()>0) {
			NameValuePair[] queryParams = new NameValuePair[params.size()];

			Iterator<String> iters = params.keySet().iterator();
			int i = 0;

			while (iters.hasNext())
			{
				String key = iters.next();
				queryParams[i] = new NameValuePair(key, params.getString(key));
				i++;
			}
			
			method.setQueryString(queryParams);
		}
		
		try
		{
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			int statusCode = method.getStatusCode();
			response.setStatusCode(statusCode+"");
			response.setResponseBody(responses);
			return response;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(e instanceof org.apache.commons.httpclient.ConnectTimeoutException){
				response.setStatusCode("408");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			} else{
				response.setStatusCode("500");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			}
		}
		finally
		{
			method.releaseConnection();
		}
		return response;
	}

	public static KKResponse urlDeleteMethod(String url, Map<String, String> params, String token)
	{
		KKResponse response = new KKResponse();
		if(token == null || "".equals(token) && url.indexOf("kitkatback/project/id") == -1){
			response.setStatusCode("500");
			response.setMessage("缺少Token认证信息");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		if(url == null || "".equals(url)) {
			response.setStatusCode("500");
			response.setMessage("请输入正确的URL");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		DeleteMethod method = new DeleteMethod(url);
		method.setRequestHeader("token", token);
		if(params != null && params.size()>0) {
			NameValuePair[] queryParams = new NameValuePair[params.size()];

			Iterator<String> iters = params.keySet().iterator();
			int i = 0;

			while (iters.hasNext())
			{
				String key = iters.next();
				queryParams[i] = new NameValuePair(key, params.get(key));
				i++;
			}

			method.setQueryString(queryParams);
		}
		
		try
		{
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			int statusCode = method.getStatusCode();
			response.setStatusCode(statusCode+"");
			response.setResponseBody(responses);
			return response;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(e instanceof org.apache.commons.httpclient.ConnectTimeoutException){
				response.setStatusCode("408");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			} else{
				response.setStatusCode("500");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			}
		}
		finally
		{
			method.releaseConnection();
		}
		return response;
	}

	public static KKResponse urlPutMethod(String url, JSONObject jsonObject, String token)
	{
		KKResponse response = new KKResponse();
		if(token == null || "".equals(token)&& url.indexOf("kitkatback/project/id") == -1){
			response.setStatusCode("500");
			response.setMessage("缺少Token认证信息");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		if(url == null || "".equals(url)) {
			response.setStatusCode("500");
			response.setMessage("请输入正确的URL");
			response.setMoreInfoUrl("support@zqsoft.com");
			return response;
		}
		
		String params = "";
		if (!"".equals(jsonObject) && null != jsonObject)
		{
			params = jsonObject.toJSONString();
		}
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
		PutMethod method = new PutMethod(url);
		try
		{
			if (params != null && !params.trim().equals(""))
			{
				RequestEntity requestEntity = new StringRequestEntity(params, "application/json", "UTF-8");
				method.setRequestEntity(requestEntity);
			}
			method.setRequestHeader("token", token);
			httpClient.executeMethod(method);
			String responses = method.getResponseBodyAsString();
			int statusCode = method.getStatusCode();
			response.setStatusCode(statusCode+"");
			response.setResponseBody(responses);
			return response;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			if(e instanceof org.apache.commons.httpclient.ConnectTimeoutException){
				response.setStatusCode("408");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			} else{
				response.setStatusCode("500");
				response.setMessage(e.getMessage());
				response.setMoreInfoUrl("support@zqsoft.com");
			}
		}
		finally
		{
			method.releaseConnection();
		}

		return response;
	}

}

/*
 * $Log: av-env.bat,v $
 */